<template>
  <div>
    <div>
      <el-breadcrumb separator-class="el-icon-arrow-right" style="font-size: 16px;">
        <el-breadcrumb-item :to="{ path: '/' }"><i class="el-icon-house" style="font-weight: bold">&nbsp;&nbsp;后台管理</i>
        </el-breadcrumb-item>
        <el-breadcrumb-item><i class="el-icon-s-marketing">&nbsp;&nbsp;仓库管理</i></el-breadcrumb-item>
        <el-breadcrumb-item><i class="el-icon-s-shop">&nbsp;&nbsp;商品管理</i></el-breadcrumb-item>
      </el-breadcrumb>
    </div>
    <div class="data-canRead" style="width: 100%;background-color: white;height: auto">
      <el-divider content-position="left">信息查询</el-divider>
      <el-form ref="form" :model="categoryForm">
        <el-form-item class="category_formSubmit_search">
          <el-select v-model="categoryForm.name" placeholder="请选择商品名称"
                     @change="">
            <el-option
                v-for="item in categoryCOptions"
                :key="item.id"
                :label="item.name"
                :value="item.id">
            </el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <el-table :data="category.slice((currentPage-1)*pageSize,currentPage*pageSize)" stripe style="width: 100%">
        <el-table-column prop="categoryId" label="商品代码" width="200px"></el-table-column>
        <el-table-column prop="name" label="商品名称" width="200px"></el-table-column>
        <el-table-column prop="lmonth" label="上月结转" width="200px"></el-table-column>
        <el-table-column prop="tmonthIn" label="本月入库" width="200px"></el-table-column>
        <el-table-column prop="tmonthOut" label="本月出库" width="200px"></el-table-column>
        <el-table-column prop="countNow" label="当前数目" width="200px"></el-table-column>
        <el-table-column prop="count" label="标准库存数" width="200px"></el-table-column>
        <el-table-column prop="overflowShort" label="溢短" width="200px"></el-table-column>
        <el-table-column prop="unitPrice" label="单价" width="200px"></el-table-column>
        <el-table-column prop="primeCost" label="成本" width="200px"></el-table-column>
        <el-table-column prop="inventoryAmount" label="库存金额" width="200px"></el-table-column>
        <el-table-column label="操作" width="200px" fixed="right">
          <template slot-scope="scope">
            <el-button
                size="mini"
                type="primary"
                circle
                @click="openEditDialog(scope.row)"><i class="el-icon-edit" style="color: white"></i>
            </el-button>
            <el-button
                size="mini"
                type="danger"
                circle
                @click="openDeleteConfirm(scope.row)"><i class="el-icon-delete" style="color: white"></i>
            </el-button>
          </template>
        </el-table-column>
        <el-table-column></el-table-column>
      </el-table>
      <div style="margin-top:15px;">
        <el-pagination align='center' @size-change="handleSizeChange"
                       @current-change="handleCurrentChange"
                       :current-page="currentPage"
                       :page-sizes="[1,5,10,20]"
                       :page-size="pageSize"
                       layout="total, sizes, prev, pager, next, jumper"
                       :total="category.length">
        </el-pagination>
      </div>

      <el-dialog
          width="80%"
          :modal-append-to-body="false"
          title="编辑商品库存"
          :visible.sync="dialogFormVisible">
        <el-form
            :model="categoryForm"
            :rules="rules"
            ref="categoryForm"
            label-width="100px"
            style="margin-right: 25px">
          <el-row>
            <el-col :span="12">
              <el-form-item label="商品代码:" prop="categoryId">
                <el-input v-model="categoryForm.categoryId" placeholder="请输入商品代码"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="商品名称:" prop="name">
                <el-input v-model="categoryForm.name" placeholder="输入商品名称"></el-input>
              </el-form-item>
            </el-col>
          </el-row>
          <el-divider content-position="left">金额计算</el-divider>
          <el-form-item label="上月结转:" prop="lmonth">
            <el-input v-model="categoryForm.lmonth" placeholder="输入上月结转"></el-input>
          </el-form-item>
          <el-row>
            <el-col :span="8">
              <el-form-item label="本月入库:" prop="tmonthIn">
                <el-input v-model="categoryForm.tmonthIn" placeholder="输入本月入库"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="本月出库:" prop="tmonthOut">
                <el-input v-model="categoryForm.tmonthOut" placeholder="输入本月出库"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="当前数目:" prop="countNow">
                <el-input v-model="categoryForm.countNow" placeholder="输入当前数目"></el-input>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="12">
              <el-form-item label="标准库存数:" prop="count">
                <el-input v-model="categoryForm.count" placeholder="输入标准库存数"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="溢短:" prop="overflowShort">
                <el-input v-model="categoryForm.overflowShort" placeholder="输入溢短"></el-input>
              </el-form-item>
            </el-col>
          </el-row>
          <el-divider content-position="left">库存金额</el-divider>
          <el-row>
            <el-col :span="12">
              <el-form-item label="单价:" prop="unitPrice">
                <el-input v-model="categoryForm.unitPrice" placeholder="输入单价"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="成本:" prop="primeCost">
                <el-input v-model="categoryForm.primeCost" placeholder="输入成本"></el-input>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="20">
              <el-form-item label="库存金额:" prop="inventoryAmount">
                <el-input v-model="categoryForm.inventoryAmount" placeholder="输入库存金额"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="4">
              <el-form-item style="float: right">
                <el-button type="primary"@click="compute" style="background-color: white;border-color: gray">
                  <i class="el-icon-thumb" style="color:gray;font-size: 100%">&nbsp;&nbsp;计算</i>
                </el-button>
              </el-form-item>
            </el-col>
          </el-row>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="dialogFormVisible = false" type="warning" style="color:white;">取 消</el-button>
          <el-button type="primary" @click="submitEditForm()" style="color:white;">确 定</el-button>
        </div>
      </el-dialog>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      dialogFormVisible:false,
      categoryCOptions: [],
      category: [],
      categoryForm: {
        categoryId: '',
        name: '',
        lmonth: '',
        tmonthIn: '',
        tmonthOut: '',
        countNow: '',
        count: '',
        overflowShort: '',
        unitPrice: '',
        primeCost: '',
        inventoryAmount: '',
      },
      rules: {
        categoryId: [
          {required: true, message: "请输入商品代码", trigger: 'blur'},
          {pattern: /^[0-9]+$/, message: "请输入数字", trigger: 'blur'}
        ],
        name: [
          {required: true, message: "请输入商品名称", trigger: 'blur'},
          {pattern: /[\u4E00-\u9FA5]/, message: "请输入中文", trigger: 'blur'}
        ],
        lmonth: [
          {required: true, message: "请输入上月结转", trigger: 'blur'},
          {pattern: /^[0-9]+$/, message: "请输入数字", trigger: 'blur'}
        ],
        tmonthIn: [
          {required: true, message: "请输入本月入库", trigger: 'blur'},
          {pattern: /^[0-9]+$/, message: "请输入数字", trigger: 'blur'}
        ],
        tmonthOut: [
          {required: true, message: "请输入本月出库", trigger: 'blur'},
          {pattern: /^[0-9]+$/, message: "请输入数字", trigger: 'blur'}
        ],
        countNow: [
          {required: true, message: "请输入当前数目", trigger: 'blur'},
          {pattern: /^[0-9]+$/, message: "请输入数字", trigger: 'blur'}
        ],
        count: [
          {required: true, message: "请输入标准库存数", trigger: 'blur'},
          {pattern: /^[0-9]+$/, message: "请输入数字", trigger: 'blur'}
        ],
        overflowShort: [
          {required: true, message: "请输入溢短", trigger: 'blur'},
          {pattern: /^[0-9]+$/, message: "请输入数字", trigger: 'blur'}
        ],
        unitPrice: [
          {required: true, message: "请输入单价", trigger: 'blur'},
          {pattern: /^[0-9]+$/, message: "请输入数字", trigger: 'blur'}
        ],
        primeCost: [
          {required: true, message: "请输入成本", trigger: 'blur'},
          {pattern: /^[0-9]+$/, message: "请输入数字", trigger: 'blur'}
        ],
        inventoryAmount: [
          {required: true, message: "请输入库存金额", trigger: 'blur'},
          {pattern: /^[0-9]+$/, message: "请输入数字", trigger: 'blur'}
        ],
        currentPage: 1,
        pageSize: 10,
      },

      currentPage: 1,
      pageSize: 10,
    }
  },
  methods: {
    compute() {
      let count = this.categoryForm.tmonthIn - this.categoryForm.tmonthOut;
      let overflowShort = this.categoryForm.tmonthOut - (this.categoryForm.tmonthIn - this.categoryForm.countNow);
      let inventory = this.categoryForm.countNow * this.categoryForm.primeCost;
      if(overflowShort<0){
        overflowShort = -overflowShort;
      }
      this.$set(this.categoryForm,'count',count);
      this.$set(this.categoryForm,'overflowShort',overflowShort);
      this.$set(this.categoryForm,'inventoryAmount',inventory);
    },
    openEditDialog(categoryForm) {
      let url = 'http://localhost:9080/wareHouseCategory/' + categoryForm.id;
      console.log('url = ' + url);

      this.axios
          .create({'headers': {'Authorization': localStorage.getItem('jwt')}})
          .get(url).then((response) => {
        let responseBody = response.data;
        if (responseBody.state == 20000) {
          this.dialogFormVisible = true;
          this.categoryForm = responseBody.data;
        } else {
          this.$alert(responseBody.message, '警告', {
            confirmButtonText: '确定',
            callback: action => {
              this.loadCategoryList();
            }
          });
        }
      });
    },
    submitEditForm() {
      let url = 'http://localhost:9080/wareHouseCategory/' + this.categoryForm.id + '/update';
      console.log('url = ' + url);
      let formData = this.qs.stringify(this.categoryForm);
      console.log('formData = ' + formData);

      this.axios
          .create({'headers': {'Authorization': localStorage.getItem('jwt')}})
          .post(url, formData).then((response) => {
        let responseBody = response.data;
        if (responseBody.state == 20000) {
          this.$message({
            message: '修改固定资产成功！',
            type: 'success'
          });
          this.loadCategoryList();
          this.dialogFormVisible = false;
        } else if (responseBody.state == 40400) {
          this.$alert(responseBody.message, '警告', {
            confirmButtonText: '确定',
            callback: action => {
              this.dialogFormVisible = false;
              this.loadCategoryList();
            }
          });
        } else {
          this.$message.error(responseBody.message);
        }
      });
    },
    openDeleteConfirm(categoryForm) {
      let message = '此操作将永久删除【' + categoryForm.name + '】商品，是否继续？';
      this.$confirm(message, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.handleDelete(categoryForm);
      }).catch(() => {
      });
    },
    handleDelete(categoryForm) {
      let url = 'http://localhost:9080/wareHouseCategory/' + categoryForm.id + '/delete';
      console.log('url = ' + url);

      this.axios
          .create({'headers': {'Authorization': localStorage.getItem('jwt')}})
          .post(url).then((response) => {
        let responseBody = response.data;
        if (responseBody.state == 20000) {
          this.$message({
            message: '删除固定资产成功！',
            type: 'success'
          });
          local.reload();
        } else if (responseBody.state == 40400) {
          this.$alert(responseBody.message, '警告', {
            confirmButtonText: '确定',
            callback: action => {
              this.loadCategoryList();
            }
          });
        } else {
          this.$message.error(responseBody.message);
        }
      });
    },
    loadCategoryList() {
      let url = 'http://localhost:9080/wareHouseCategory';
      console.log('url = ' + url);

      this.axios
          .create({'headers': {'Authorization': localStorage.getItem('jwt')}})
          .get(url).then((response) => {
        let responseBody = response.data;
        console.log('state=' + responseBody.state);
        console.log('message=' + responseBody.message);
        this.category = responseBody.data;
        console.log('fixedAssets=' + responseBody.data);
      });
    },
    /*表格*/
    //每页条数改变时触发 选择一页显示多少行
    handleSizeChange(val) {
      console.log(`每页 ${val} 条`);
      this.currentPage = 1;
      this.pageSize = val;
    },
    //当前页改变时触发 跳转其他页
    handleCurrentChange(val) {
      console.log(`当前页: ${val}`);
      this.currentPage = val;
    },
  },
  mounted() {
    this.loadCategoryList();
  }
}
</script>

<style scoped>
* {
  color: black;
}

.data-canRead {
  width: 380px;
  height: 380px;
  margin-top: 15px;
  box-shadow: 0px 5px 5px #8d858a;
  float: left;
  margin-right: 16px;
}

.category_formSubmit_search {
  width: 200px;
  float: left;
  right: 10px;
  background-color: white;
}

</style>